﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace LocalSend2 {
	internal static class Program {

		[DllImport("user32.dll")]
		[return: MarshalAs(UnmanagedType.Bool)]
		static extern bool SetForegroundWindow(IntPtr hWnd);

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() {

			// 应用单例模式
			Process self = Process.GetCurrentProcess();
			Process[] runningProcessList = Process.GetProcesses();
			Process uInstance = runningProcessList.Where(o => o.ProcessName == self.ProcessName && o.Id != self.Id).FirstOrDefault();
			// 已存在该应用实例
			if (uInstance != null) {
				// 实例窗口前置
				//MessageBox.Show(uInstance.ToString());
				SetForegroundWindow(uInstance.MainWindowHandle);
				self.Kill();
				return;
			}

			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());
		}
	}
}
